﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Model;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace DAL
{
    public class PositionDAL
    {
        /// <summary>
        /// 链接数据库
        /// </summary>
        Database db = DatabaseFactory.CreateDatabase("constring");
        /// <summary>
        /// 显示职位
        /// </summary>
        /// <returns></returns>
        public List<Position> ShowPosition(int pageindex, int pagesize)
        {
            try
            {
                if (pageindex < 1)
                {
                    pageindex = 1;
                }
                int start = (pageindex - 1) * pagesize + 1;
                int end = pageindex * pagesize;
                string sql = "SELECT p.position_id, p.position_name FROM position AS p";
                DataSet ds = db.ExecuteDataSet(CommandType.Text, sql);
                List<Position> list = new List<Position>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Position posi = new Position();
                    posi.position_id = (int)dr["position_id"];
                    posi.position_name = dr["position_name"].ToString();
                    list.Add(posi);
                }
                return list;
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// 获取职位总数
        /// </summary>
        /// <returns></returns>
        public int CountPosition()
        {
            try
            {
                int dr = Convert.ToInt32(db.ExecuteNonQuery(CommandType.Text, " SELECT COUNT(*) FROM position AS p "));
                return dr;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 删除职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelPosition(int id)
        {
            try
            {
                int dr = Convert.ToInt32(db.ExecuteNonQuery(CommandType.Text, "DELETE FROM position WHERE position_id=" + id));
                return dr;
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}
